import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler


def preprocess():
    data = pd.read_csv("../covid_19_data.csv")
    data['ObservationDate'] = pd.to_datetime(data['ObservationDate'])
    data = data.drop(['Country/Region', 'Last Update',
                      'SNo', 'Province/State'], axis=1)
    temp = data.groupby(['ObservationDate']).agg(
        {'Confirmed': 'sum', 'Deaths': 'sum', 'Recovered': 'sum'}).reset_index()
    data = temp.filter(['Recovered'])
    dataset = data.values

    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(dataset)

    training_data_len = int(np.ceil(len(dataset) * .8))
    train_data = scaled_data[0:int(training_data_len), :]
    # Split the data into x_train and y_train data sets
    x_train = []
    y_train = []

    for i in range(30, len(train_data)):
        x_train.append(train_data[i-30:i, 0])
        y_train.append(train_data[i, 0])

    x = np.asarray(x_train)
    x = x.reshape(x.shape[0], 30, 1)

    y = np.asarray(y_train)
    y = y.reshape(y.shape[0], 1)

    test_data = scaled_data[training_data_len - 30:, :]
    # Create the data sets x_test and y_test
    x_test = []
    y_test = dataset[training_data_len:, :]
    #y_test = y_test.reshape(1, y_test.shape[0])
    for i in range(30, len(test_data)):
        x_test.append(test_data[i-30:i, 0])

    # Convert the data to a numpy array
    x_test = np.array(x_test)
    x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

    return data, x, y, x_test, y_test
